<?php

namespace app\dao\exam;

use app\dao\BaseDao;
use app\model\exam\ExamRecord;
use app\model\office\OfficeChapter;
use app\model\office\OfficeRules;
use app\model\office\OfficeStudyRecord;
use think\Exception;

class ExamRecordDao extends BaseDao
{

    public function index(array $where)
    {
        return $this->getModel()
            ->with([
                'OfficeClass' => function ($query) {
                    $query->withField(['id', 'title']);
                }, 'majors' => function ($query) {
                    $query->withField(['id', 'title']);
                }])
            ->withSearch(['idcard', 'user_name', 'submit_type', 'exam_type', 'read_status', 'exam_id'], [
                'idcard' => $where['idcard'],
                'user_name' => $where['user_name'],
                'submit_type' => $where['submit_type'],
                'exam_type' => $where['exam_type'],
                'read_status' => $where['read_status'],
                'exam_id' => $where['exam_id']
            ])
            ->where('website_id', $where['website_id'])
            ->order('id', 'desc')
            ->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        unset($where['id']);
        return $this
            ->getModel()
            ->create($where);
    }

    public function updateDao(array $where)
    {
        unset($where['website_id']);
        return $this->getModel()->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()->whereIn('id', $where['id'])->update(['delete_time' => date('Y-m-d H:i:s', time())]);
    }

    public function MarkingPapers(array $where)
    {
        return $this->getModel()->update($where);
    }

    protected function setModel(): string
    {
        return ExamRecord::class;
    }

    //-----------------------------------------------api-------------------------------------------------------
    /*
     * 验证学生是否保存过
     */
    public function verifyData(array $where)
    {

        try {
            return $this->getModel()
                ->where('idcard', $where['idcard'])
                ->where('class_id', $where['class_id'])
                ->where('exam_id', $where['exam_id'])
                ->find();
        } catch (Exception $exception) {
            return null;
        }

    }

    /*
     * 学生考试记录保存
     */
    public function testRecord(array $where)
    {
        return $this->getModel()->create($where);
    }

    /*
     * 学生考试记录更新
     */
    public function testUpdate(array $where)
    {
        unset($where['website_id']);
        return $this->getModel()
            ->where('idcard', $where['idcard'])
            ->where('class_id', $where['class_id'])
            ->where('exam_id', $where['exam_id'])
            ->update($where);
    }

    /*
     * 获取考试分数
     */
    public function getScore(array $where)
    {
        return $this->getModel()
            ->with(['exam' => function ($query) {
                $query->field(['id', 'title']);
            }])
            ->where('idcard', $where['idcard'])
            ->where('class_id', $where['class_id'])
            ->where('website_id', $where['website_id'])
            ->where('read_status', 1)
            ->where('status', 1)
            ->field(['id', 'exam_id', 'total_score', 'exam_type'])
            ->select();

    }

    /*
     * 获取视频分数
     */
    public function getVideoRecord(array $where)
    {
        return OfficeStudyRecord::where('website_id', $where['website_id'])
            ->where('class_id', $where['class_id'])
            ->where('user_id', $where['user_id'])
            ->where('is_pass', 1)
            ->field(['id'])
            ->select();
    }

    public function getVideo(array $where)
    {
        return OfficeChapter::where('class_id', $where['class_id'])
            ->where('website_id', $where['website_id'])
            ->field('id')
            ->select();
    }

    /*
     * 获取课程考核规则
     */
    public function getRules(array $where)
    {
        return OfficeRules::where('class_id', $where['class_id'])
            ->where('website_id', $where['website_id'])
            ->field(['id', 'video_ratio', 'work_ratio', 'text_ratio'])
            ->find();
    }
}